<template>
  <div class="admin-login-container">
    <div class="login-box">
      <div class="login-header">
        <img src="@/assets/logo.png" alt="Logo" class="logo">
        <h2>服装定制管理系统</h2>
        <div class="subtitle">管理员登录</div>
      </div>
      
      <el-form 
        ref="loginFormRef" 
        :model="loginForm" 
        :rules="rules" 
        class="login-form" 
        label-position="top"
      >
        <el-form-item prop="username" label="用户名">
          <el-input 
            v-model="loginForm.username" 
            prefix-icon="User" 
            placeholder="请输入用户名" 
            clearable
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        
        <el-form-item prop="password" label="密码">
          <el-input 
            v-model="loginForm.password" 
            prefix-icon="Lock" 
            placeholder="请输入密码" 
            show-password
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        
        <div class="remember-row">
          <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
          <el-link type="primary" :underline="false">忘记密码?</el-link>
        </div>
        
        <el-form-item>
          <el-button 
            type="primary" 
            class="login-button" 
            :loading="loading" 
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-footer">
        <p>© {{ new Date().getFullYear() }} 服装定制管理系统 - 管理员端</p>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import request from '@/utils/request'

export default {
  name: 'Login',
  setup() {
    const router = useRouter()
    const loginFormRef = ref(null)
    const loading = ref(false)
    
    // 登录表单
    const loginForm = reactive({
      username: '',
      password: '',
      role: 'ADMIN'
    })
    
    // 表单校验规则
    const rules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
      ]
    }
    
    // 从本地存储获取保存的用户名和密码
    onMounted(() => {
      const savedUsername = localStorage.getItem('admin_username')
      const savedPassword = localStorage.getItem('admin_password')
      
      if (savedUsername) {
        loginForm.username = savedUsername
        loginForm.remember = true
        
        if (savedPassword) {
          loginForm.password = savedPassword
        }
      }
    })
    
    // 处理登录
    const handleLogin = async () => {
      if (!loginFormRef.value) return
      
      await loginFormRef.value.validate(async (valid) => {
        if (valid) {
          loading.value = true
          
          try {
            const res = await request.post('/api/auth/login', loginForm)
            localStorage.setItem('token', res.data.token)
            localStorage.setItem('userInfo', JSON.stringify({
              username: res.data.username,
              role: res.data.role,
              userId: res.data.userId
            }))
            
            // 如果选择了记住我，保存账号
            if (loginForm.remember) {
              localStorage.setItem('admin_username', loginForm.username)
            } else {
              localStorage.removeItem('admin_username')
              localStorage.removeItem('admin_password')
            }
            
            ElMessage.success('登录成功')
            router.push('/admin/dashboard')
          } catch (error) {
            console.error('登录失败:', error)
            ElMessage.error('登录失败，请稍后重试')
          } finally {
            loading.value = false
          }
        }
      })
    }
    
    return {
      loginFormRef,
      loginForm,
      rules,
      loading,
      handleLogin
    }
  }
}
</script>

<style scoped>
.admin-login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-box {
  width: 100%;
  max-width: 440px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  padding: 40px;
  box-sizing: border-box;
  text-align: center;
}

.login-header {
  margin-bottom: 30px;
}

.logo {
  width: 80px;
  height: 80px;
  object-fit: contain;
  margin-bottom: 16px;
}

.login-header h2 {
  font-size: 24px;
  color: #333;
  margin: 0;
  margin-bottom: 8px;
}

.subtitle {
  font-size: 16px;
  color: #666;
  margin-bottom: 24px;
}

.login-form {
  text-align: left;
}

.remember-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.login-button {
  width: 100%;
  height: 44px;
  border-radius: 4px;
  font-size: 16px;
  font-weight: 500;
}

.login-footer {
  margin-top: 20px;
  font-size: 14px;
  color: #999;
}

/* 响应式调整 */
@media (max-width: 480px) {
  .login-box {
    padding: 30px 20px;
  }
  
  .logo {
    width: 60px;
    height: 60px;
  }
  
  .login-header h2 {
    font-size: 20px;
  }
  
  .subtitle {
    font-size: 14px;
  }
}
</style> 